package top.kaoshanji.p2ch16a;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 代码清单-p2ch16a-472页a：使用AtomicInteger实现锁
 * author kaoshanji
 * ^_^
 * create time 2025/2/26 16:25
 */
public class MyLock {

    private AtomicInteger status = new AtomicInteger(0);

    public void lock() {
        while (!status.compareAndSet(0, 1)) {
            Thread.yield();
        }
    }

    public void unLock() {
        status.compareAndSet(1, 0);
    }

}
